package com.quanjiaolian.order.serviceimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.quanjiaolian.DB.DataBase;
import com.quanjiaolian.order.service.OrderService;
import com.quanjiaolian.tools.DateDistance;

public class OrderServiceImpl implements OrderService {
	/**
	 * 声明Log4j变量
	 */
	public static ResourceBundle resourceBundle = null;
	static Logger logger = Logger.getLogger(OrderServiceImpl.class.getName());
	
	DataBase db = new DataBase();
 /**
   * 声明数据库连接实例
   */
	//定义一个Connection 用来连接数据库 
	Connection conn=null;  
  //声明PreparedStatement实例
  PreparedStatement pstmt = null;
  //用于存放返回结果的数据集
  ResultSet rs = null;
	@SuppressWarnings("static-access")
	public int insertOrder(Map<String, Object> map) {
		int i = 0;
		String sql = "insert into order_info values (?,?,?,?,?,?,?,?,?,?,?)";
		try {
			conn = db.getconn();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1,Integer.parseInt(map.get("ins_id").toString()));
			pstmt.setInt(2,Integer.parseInt(map.get("user_id").toString()));
			pstmt.setString(3,map.get("order_id").toString());
			pstmt.setString(4,map.get("order_date").toString());
			pstmt.setString(5,map.get("test_date").toString());
			pstmt.setString(6,map.get("test_time").toString());
			pstmt.setString(7,map.get("test_addr").toString());
			pstmt.setString(8,map.get("teach_way").toString());
			pstmt.setString(9,map.get("order_pay").toString());
			pstmt.setString(10,map.get("user_pay").toString());
			pstmt.setInt(11,Integer.parseInt(map.get("order_state").toString()));
			i = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.close(conn, pstmt, rs);
		}
		return i;
	}
	
	@SuppressWarnings("static-access")
	public int updateOrderState(String order_id,int order_state){
		int i = 0;
		String sql = "update order_info set order_state = ? where order_id = ?";
		try {
			conn = db.getconn();
			pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1,order_state);
			pstmt.setString(2,order_id);
			i = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.close(conn, pstmt, rs);
		}
		return i;
	}
	
	@SuppressWarnings("static-access")
	public int deleteOrderById(String order_id){
		int i = 0;
		String sql = "delete from order_info where order_id = ?";
		try {
			conn = db.getconn();
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1,order_id);
			i = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.close(conn, pstmt, rs);
		}
		return i;
	}
	
	@SuppressWarnings("static-access")
	public void makeOrderFailed(){
		List<Object> list = new ArrayList<Object>();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sql = "select oi.order_id,oi.order_date from order_info oi where oi.order_state = 2";
		try {
			conn = db.getconn();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			while(rs.next()){
				long[] lon =  DateDistance.getDistanceTimes(rs.getString("order_date"),sdf.format(new Date()));
				if(lon[2]>30){
					list.add(rs.getString("order_id"));
				}
			}
			if(list.size()>0){
				boolean b = updateState(list);
				if(b){
					logger.info("*****************批量失效订单处理成功！*****************");
				}else{
					logger.info("*****************批量失效订单处理失败！*****************");
				}
			}else{
				logger.info("*****************本次任务中没有发现失效的订单！*****************");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.close(conn, pstmt, rs);
		}
	}
	/**
	 * 根据订单编号列表更新数据
	 * @param list
	 * @return true为批量更新成功 反之则为失败
	 */
	@SuppressWarnings("static-access")
	public boolean updateState(List<Object> list){
		boolean b = false;
		StringBuffer sb = new StringBuffer();
		sb.append("(");
		for(int i=0;i<list.size();i++){
			sb.append(list.get(i));
			if(list.size() != 1 && i<list.size()-1){
				sb.append(",");
			}
		}
		sb.append(")");
		logger.info("本次任务中将失效的订单编号列表为："+sb.toString());
		String sql = "update order_info set order_state = 3 where order_id in "+sb.toString();
		try {
			conn = db.getconn();
			pstmt = conn.prepareStatement(sql);
			int i = pstmt.executeUpdate();
			if(i>0){
				b = true;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			db.close(conn, pstmt, rs);
		}
		return b;
	}
	

}
